package com.mlst.smugmuglivewallpaper;

import java.io.StringReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class RSSParser {

    public static ArrayList<String> parseItems(String s) {
	try {
	    final XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
	    xpp.setInput(new StringReader(s));

	    int eventType = xpp.getEventType();
	    String currentText = "";

	    final ArrayList<String> items = new ArrayList<String>();
	    boolean inItem = false;
	    while (eventType != XmlPullParser.END_DOCUMENT) {
		final String name = xpp.getName();
		if (eventType == XmlPullParser.START_TAG) {
		    if (name.equals("item")) {
			inItem = true;
		    }

		    if (name.equals("media:content") && inItem) {
			if (xpp.getAttributeCount() == 7) {
			    items.add(xpp.getAttributeValue(0));
			    inItem = false;
			}
		    }

		} else if (eventType == XmlPullParser.TEXT) {
		    currentText += xpp.getText();
		}

		eventType = xpp.next();
	    }

	    return items;
	} catch (Exception e) {
	    e.printStackTrace();
	}

	return null;
    }
}
